package com.tcm.user.event;

import com.base.core.utils.R;
import com.tcm.im.service.HxService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class LoginHxRegisterListener implements ApplicationListener<LoginEvent> {


    @DubboReference(version = "0.0.1-alpha", check = false, timeout = 3000)
    private HxService hxService;

    private static final Logger LOGGER = LoggerFactory.getLogger(LoginHxRegisterListener.class);

    @Override
    public void onApplicationEvent(LoginEvent loginEvent) {
        Map<String,Object> params = loginEvent.getLoginParams();
        if(null == params || "".equals(params)){
            R.throwFail("非法参数");
        }
        Long userId = (Long)params.get("userId");
        if(null == userId || "".equals(userId)){
            LOGGER.info("[LoginHxRegisterListener]:userId不存在");
            R.throwFail("非法参数");
        }
        boolean rs = false;
        try {
            rs = hxService.addUser(userId.toString());
            if(!rs){
                LOGGER.info("环信新注册失败", loginEvent.getLoginParams().toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
